/*------------------------------------------------------------------------------

Publication: Schakel, W. (2020) Representing the Rich: Economic and Political
	Inequality in Established Democracies. University of Amsterdam: Doctoral
	Dissertation.

Chapter: 2

Description: This do-file constructs the dataset used for the analyses in
	chapter 2. NOTE: IT IS NOT NECESSARY TO RUN THIS DO-FILE TO REPLICATE THE
	CHAPTER, BECAUSE THIS DO-FILE CONSTRUCTS THE SCHAKEL_2020_CHAPTER2_1_DATA
	FILE WHICH IS PROVIDED WITH THE SUPPLEMENTARY MATERIAL. However, the file is
	still included with the replication materials for the sake of transparancy,
	as it shows how the various sources were transformed into the data file used
	in the analyses.

Sections:

	1. Harmonizing the survey data: ISSP 1985
	2. Harmonizing the survey data: ISSP 1990
	3. Harmonizing the survey data: ISSP 1996
	4. Harmonizing the survey data: ISSP 2006
	5. Calculating spending preferences by income
	6. Adding data on welfare generosity
	7. Adding data on government spending
	8. Adding data on GDP, growth and unemployment
	9. Adding further country-level variables

Data: ISSP Role of Government I-IV, downloaded in April 2019 from Gesis at:
	https://dbk.gesis.org/dbksearch/sdesc2.asp?no=1490 (Role of Government I)
	https://dbk.gesis.org/dbksearch/sdesc2.asp?no=1950 (Role of Government II)
	https://dbk.gesis.org/dbksearch/sdesc2.asp?no=2900 (Role of Government III)
	https://dbk.gesis.org/dbksearch/sdesc2.asp?no=4700 (Role of Government IV)

	Comparative Welfare Entitlements Dataset, downloaded in April 2019 from:
	http://cwed2.org/

	OECD Social Expenditure database, downloaded in April 2019 from:
	https://stats.oecd.org/Index.aspx?DataSetCode=SOCX_AGG

	World Bank data on GDP, growth and unemployment, downloaded in April 2019 from:
	http://data.worldbank.org/indicator/NY.GDP.PCAP.KD (GDP per capita)
	http://data.worldbank.org/indicator/NY.GDP.PCAP.KD.ZG (Economic growth)
	http://data.worldbank.org/indicator/SL.UEM.TOTL.NE.ZS (Unemployment)

	Quality of Government Standard Dataset, downloaded in April 2019 from:
	http://qog.pol.gu.se/data/datadownloads/qogstandarddata (Time-series Data)	

	Comparative Political Data Set, downloaded in April 2019 from:
	http://www.cpds-data.org/index.php/data	

------------------------------------------------------------------------------*/

global data "YOUR PATH HERE"

/*------------------------------------------------------------------------------
 1. Harmonizing the survey data: ISSP 1985
------------------------------------------------------------------------------*/

use "$data\ZA1490.dta", clear

* Identifying variables
gen ctemp=V3
label define clab 1 "Australia" 2 "Germany" 3 "Great Britain"				///
	4 "United States" 5 "Austria" 8 "Italy", replace
label values ctemp clab
decode ctemp, gen(country)
gen wave=1
gen year=1985
replace year=1986 if country=="Austria" | country=="Australia"

* The values of the weights in 1985 are 100000 times as large as in other years
gen weight=V141/100000

* Spending preferences
recode V83 V87 V88 (1=100) (2=50) (3=0) (4=-50) (5=-100), gen(heal pens unem)

* Welfare state values and budget-cutting preferences
recode V101 V107 (4=-100) (3=-33.33) (2=33.33) (1=100), gen(V101_1 V107_1)
gen wsv=(V101_1+V107_1)/2

recode V76 (1=100) (2=50) (3=0) (4=-50) (5=-100), gen(bcp)

* Income
* For Germany, we use respondent income because family income is missing.
gen inc=V128
replace inc=V129 if country=="Germany"

keep country wave year weight heal pens unem wsv bcp inc

tempfile issp85
save `issp85', replace

/*------------------------------------------------------------------------------
 2. Harmonizing the survey data: ISSP 1990
------------------------------------------------------------------------------*/

use "$data\ZA1950.dta", clear

recode v3 (3=2), gen(ctemp) // We combine East and West Germany
label define clab 1 "Australia" 2 "Germany" 4 "Great Britain" 5				///
	"Northern Ireland" 6 "United States" 7 "Hungary" 8 "Italy" 9 "Ireland"	///
	10 "Norway" 11 "Israel", replace
label values ctemp clab
decode ctemp, gen(country)
gen wave=2
gen year=1990
replace year=1991 if country=="Ireland" | country=="Israel"
gen weight=v114

recode v34 v38 v39 (1=100) (2=50) (3=0) (4=-50) (5=-100), gen(heal pens unem)

recode v49 v55 (4=-100) (3=-33.33) (2=33.33) (1=100), gen(v49_1 v55_1)
gen wsv=(v49_1+v55_1)/2

recode v27 (1=100) (2=50) (3=0) (4=-50) (5=-100), gen(bcp)

gen inc=v100

keep country wave year weight heal pens unem wsv bcp inc

tempfile issp90
save `issp90', replace

/*------------------------------------------------------------------------------
 3. Harmonizing the survey data: ISSP 1996
------------------------------------------------------------------------------*/

use "$data\ZA2900.dta", clear

recode v3 (3=2) (23=22), gen(ctemp) // We also combine different samples from Israel
label define clab 1 "Australia" 2 "Germany" 4 "Great Britain" 6				///
	"United States" 8 "Hungary" 9 "Italy" 10 "Ireland" 12 "Norway" 13		///
	"Sweden" 14 "Czech Republic" 15 "Slovenia" 16 "Poland" 17 "Bulgaria"	///
	18 "Russia" 19 "New Zealand" 20 "Canada" 21 "Philippines" 22 "Israel"	///
	24 "Japan" 25 "Spain" 26 "Latvia" 27 "France" 28 "Cyprus" 30			///
	"Switzerland", replace
label values ctemp clab
decode ctemp, gen(country)
gen wave=3
gen year=1996
replace year=1997 if inlist(country,"Australia","Bulgaria","France",		///
	"New Zealand","Poland","Russia")
replace year=1998 if country=="Switzerland"
gen weight=v325

recode v26 v30 v31 (1=100) (2=50) (3=0) (4=-50) (5=-100), gen(heal pens unem)

recode v36 v42 (4=-100) (3=-33.33) (2=33.33) (1=100), gen(v36_1 v42_1)
gen wsv=(v36_1+v42_1)/2
recode v19 (1=100) (2=50) (3=0) (4=-50) (5=-100), gen(bcp)

gen inc=v218

keep country wave year weight heal pens unem wsv bcp inc

tempfile issp96
save `issp96', replace

/*------------------------------------------------------------------------------
 4. Harmonizing the survey data: ISSP 2006
------------------------------------------------------------------------------*/

use "$data\ZA4700.dta", clear

decode V3a, gen(ctemp)
gen country=substr(ctemp,4,.)
gen wave=4
gen year=2006
replace year=2007 if inlist(country,"Australia","Israel","Latvia",			///
	"Russia","Spain","Switzerland")
replace year=2008 if inlist(country,"Denmark","Poland")

recode V18 V22 V23 (1=100) (2=50) (3=0) (4=-50) (5=-100), gen(heal pens unem)

recode V25 V31 (4=-100) (3=-33.33) (2=33.33) (1=100), gen(V25_1 V31_1)
gen wsv=(V25_1+V31_1)/2
recode V11 (1=100) (2=50) (3=0) (4=-50) (5=-100), gen(bcp)

gen inc=.

foreach var of varlist AU_INC-ZA_INC {
	replace inc=`var' if `var'!=.
	}

keep country wave year weight heal pens unem wsv bcp inc

tempfile issp06
save `issp06', replace

* Now we merge the datasets
use `issp85', clear
append using `issp90'
append using `issp96'
append using `issp06'

tempfile issp
save `issp', replace

* We only merge in the OECD spending data here to identify and drop
* country-years from the ISSP that can't be matched to the OECD data (or CWED).
use "$data\OECD_2019.dta", clear
replace country="Great Britain" if country=="United Kingdom"
replace country="Slovakia" if country=="Slovak Republic"
replace country="South Korea" if country=="Korea"
tempfile oecd
save `oecd', replace

use `issp'
merge m:1 country using `oecd'
drop if (_merge==1 | _merge==2) & country!="Bulgaria" & country!="Taiwan"
drop pc_health1980-_merge

* Ireland 1991 has all missing values on the spending questions
drop if country=="Ireland" & year==1991

* Israel 1996 has all missing values on income
drop if country=="Israel" & year==1996

sort wave country year
egen cy=group(wave country year)

save `issp', replace

/*------------------------------------------------------------------------------
 5. Calculating spending preferences by income
------------------------------------------------------------------------------*/

sum cy
scalar cy=r(max)

quietly forval cy=1/`=scalar(cy)' {
	use `issp', clear
	keep if cy==`cy'

	* Overall preferences
	tabstat heal pens unem [aw=weight], s(mean) save
	matrix all=r(StatTotal)'
	svmat all

	* Terciles
	xtile inc3w=inc [pw=weight], n(3)
	tabstat heal pens unem [aw=weight], by(inc3w) s(mean) save
	matrix t=r(Stat1)',r(Stat2)',r(Stat3)'
	svmat t

	* Quintiles (the if-statements are only necessary because in some country-
	* years, the xtile command produces four groups instead of five)
	xtile inc5w=inc [pw=weight], n(5)

	if inlist(cy,5,11,17,19,31,35,36,50,53,56) {
		drop inc5w
		}

	if inlist(cy,17,19,35,36,50,53,56) {
		xtile inc5w=inc [pw=weight], n(6)
		}

	if inlist(cy,5) {
		recode inc (1/3=1) (4=2) (5 6=3) (7 8=4) (9/max=5), gen(inc5w)
		}

	if inlist(cy,11) {
		gen inc5w=inc
		}

	if inlist(cy,31) {
		recode inc (40000=1) (75000=2) (125000=3) (175000=4) (250000/max=5), gen(inc5w)
		}

	if inlist(cy,19,53) {
		recode inc5w (5=4) (6=5)
		}

	if inlist(cy,56) {
		recode inc5w (6=5)
		}

	tabstat heal pens unem [aw=weight], by(inc5w) s(mean) save
	matrix q=r(Stat1)',r(Stat2)',r(Stat3)',r(Stat4)',r(Stat5)'
	svmat q

	* Predicted values
	egen inc20=cut(inc), group(20)
	distinct inc
	replace inc=inc20+1 if r(ndistinct)>20

	egen incgroup=group(inc)
	replace inc=incgroup

	egen inctot=count(inc)
	gen incm=.
	sum inc

	forval b=1/`r(max)' {
		egen incn`b'=count(inc) if inc==`b'
		sum incn`b'
		replace incn`b'=`r(mean)'
		egen incc`b'=rowtotal(incn*)
		}

	rename incc# incc#, renumber(2)
	gen incc1=0
	sum inc

	forval c=1/`r(max)' {
		gen incm`c'=(incc`c' + (incn`c'/2))/inctot
		replace incm=incm`c' if inc==`c'
		}

	recode heal pens unem (.=.), gen(topic1 topic2 topic3)

	foreach p in 05 10 30 50 70 90 95 {
		gen p`p'=.
		}

	forval x=1/3 {
		reg topic`x' c.incm##c.incm [pw=weight]
		margins, at(incm=(.05 .10 .30 .50 .70 .90 .95))
		matrix b=r(b)
		replace p05=b[1,1] in `x'
		replace p10=b[1,2] in `x'
		replace p30=b[1,3] in `x'
		replace p50=b[1,4] in `x'
		replace p70=b[1,5] in `x'
		replace p90=b[1,6] in `x'
		replace p95=b[1,7] in `x'
		}

	* Welfare state values and budget-cutting preferences
	foreach var of varlist wsv bcp {
		sum `var' [aw=weight]
		gen `var'all=r(mean)

		reg `var' c.incm##c.incm [pw=weight]
		margins, at(incm=(.05 .10 .30 .50 .70 .90 .95))
		matrix b=r(b)
		gen `var'05=b[1,1]
		gen `var'10=b[1,2]
		gen `var'30=b[1,3]
		gen `var'50=b[1,4]
		gen `var'70=b[1,5]
		gen `var'90=b[1,6]
		gen `var'95=b[1,7]
		}

	keep country wave year cy all1 t1-t3 q1-q5 p05-bcp95
	keep in 1/3
	gen topic=_n

	tempfile cy`cy'
	save "`cy`cy''"
	}

clear

forval cy=1/`=scalar(cy)' {
	append using "`cy`cy''"
	}

gen id=_n
rename all1 all
rename cy countryyear
rename country country2
encode country2, gen(country)
egen countrytopic=group(country topic)
order id country country2 year wave topic countryyear countrytopic, first

label define topic 1 "Health" 2 "Pensions" 3 "Unemployment", replace
label values topic topic
tempfile issp
save `issp', replace

/*------------------------------------------------------------------------------
 6. Adding data on welfare generosity
------------------------------------------------------------------------------*/

* Note: we downloaded all variables, countries and years from the CWED website
* and copied the data into Stata.
use "$data\cwed-subset.dta", clear

rename country country2
rename skgen gent1
rename pgen gent2
rename uegen gent3

* For Cyprus 1996 and Hungary 1990, we use the replacement rates gathered by
* Van Vliet and Caminada here: https://www.universiteitleiden.nl/en/law/
* institute-for-tax-law-and-economics/economics/data-sets/unemployment-
* replacement-rates-dataset
set obs 1392
replace country2="Cyprus" in 1387/1392
replace year=_n+609 in 1387/1392
recode year (1996=.609) (1997=.613) (1998=.613) (1999=.618) (2000=.605)		///
	(2001=.606), gen(us100_c)
recode year (1996=.755) (1997=.778) (1998=.778) (1999=.783) (2000=.774)		///
	(2001=.775), gen(uc1000_c)

recode year (1990=.721) (1991=.632) (1992=.701) (1993=.677) (1994=.659)		///
	(1995=.604), gen(us100_h)
recode year (1990=.803) (1991=.731) (1992=.786) (1993=.762) (1994=.738)		///
	(1995=.692), gen(uc1000_h)

replace us100=us100_c if country2=="Cyprus"
replace uc1000=uc1000_c if country2=="Cyprus"
replace us100=us100_h if country2=="Hungary" & us100_h<1000
replace uc1000=uc1000_h if country2=="Hungary" & us100_h<1000

foreach var of varlist ss100 sc1000 mps100 mpc1000 us100 uc1000 {
	egen `var'_std=std(`var')
	}

gen pgent1=(ss100_std+sc1000_std)/2
gen pgent2=(mps100_std+mpc1000_std)/2
gen pgent3=(us100_std+uc1000_std)/2
replace pgent1=pgent1+3
replace pgent2=pgent2+3
replace pgent3=pgent3+3

keep country2 ccode year gent* pgent*
order gent3, after(gent2)

replace country2="Great Britain" if country=="United Kingdom"
replace country2="South Korea" if country=="Korea"
drop if year<1980

xtset ccode year

* Here we calculate the average change in generosity for different time periods
* both after and before the year of the survey.
forval x=1/5 {
	forval y=1/3 {
		gen gent`x'`y'=F`x'.gent`y'
		gen pgent`x'`y'=F`x'.pgent`y'
		gen dgent`x'`y'=(F`x'.gent`y'-gent`y')/gent`y'*100
		gen dpgent`x'`y'=(F`x'.pgent`y'-pgent`y')/pgent`y'*100
		}

	forval y=1/3 {
		gen gentminus`x'`y'=L`x'.gent`y'
		gen dgentminus`x'`y'=(gent`y'-L`x'.gent`y')/gent`y'*100
		}
	}

egen countryyear=group(country2 year)
reshape long gent gent1 gent2 gent3 gent4 gent5 pgent pgent1 pgent2 pgent3	///
	pgent4 pgent5 dgent1 dgent2 dgent3 dgent4 dgent5 dpgent1 dpgent2		///
	dpgent3 dpgent4 dpgent5 gentminus1 gentminus2 gentminus3 gentminus4		///
	gentminus5 dgentminus1 dgentminus2 dgentminus3 dgentminus4 dgentminus5,	///
	i(countryyear) j(topic)

egen dgentav13=rowmean(dgent1 dgent2 dgent3)
egen dgentav14=rowmean(dgent1 dgent2 dgent3 dgent4)
egen dgentav15=rowmean(dgent1 dgent2 dgent3 dgent4 dgent5)
egen dpgentav13=rowmean(dpgent1 dpgent2 dpgent3)
egen dpgentav14=rowmean(dpgent1 dpgent2 dpgent3 dpgent4)
egen dpgentav15=rowmean(dpgent1 dpgent2 dpgent3 dpgent4 dpgent5)
egen dgentminus14=rowmean(dgentminus1 dgentminus2 dgentminus3 dgentminus4)

egen miss3=rowmiss(dgent1 dgent2 dgent3)
egen miss4=rowmiss(dgent1 dgent2 dgent3 dgent4)
egen miss5=rowmiss(dgent1 dgent2 dgent3 dgent4 dgent5)
egen pmiss3=rowmiss(dpgent1 dpgent2 dpgent3)
egen pmiss4=rowmiss(dpgent1 dpgent2 dpgent3 dpgent4)
egen pmiss5=rowmiss(dpgent1 dpgent2 dpgent3 dpgent4 dpgent5)

replace dgentav13=. if miss3>1
replace dgentav14=. if miss4>1
replace dgentav15=. if miss5>1
replace dpgentav13=. if pmiss3>1
replace dpgentav14=. if pmiss4>1
replace dpgentav15=. if pmiss5>1

drop dgentminus1 dgentminus2 dgentminus3 dgentminus4 dgentminus5 miss* pmiss*

recode dgent* (.=0) if gent==0 & gent5==0
order gent*, alpha last
order dgent*, alpha last
order pgent*, alpha last
order dpgent*, alpha last
drop countryyear ccode

tempfile cwed2
save `cwed2', replace

use `issp', clear

merge 1:1 country2 topic year using `cwed2'
drop if _merge==2
drop _merge
sort id

save `issp', replace

/*------------------------------------------------------------------------------
 7. Adding data on government spending
------------------------------------------------------------------------------*/

/* Note: we downloaded data on public and mandatory private spending on health,
old age and unemployment per capita (at constant prices and PPPs) and as a
percentage of GDP from the Social Expenditure database, which we put on one
Excel sheet and copied into Stata. */
use "$data\OECD_2019.dta", clear

rename country country2
replace country2="Great Britain" if country=="United Kingdom"
replace country2="Slovakia" if country=="Slovak Republic"
replace country2="South Korea" if country=="Korea"
replace pc_pensions1986=pc_pensions1985 if country=="Austria"
replace pc_unemp1986=pc_unemp1985 if country=="Austria"

rename pc_health* vart1*
rename pc_pensions* vart2*
rename pc_unemp* vart3*
rename pgdp_health* vart4*
rename pgdp_pensions* vart5*
rename pgdp_unemp* vart6*

reshape long vart1 vart2 vart3 vart4 vart5 vart6, i(country2) j(year)
encode country2, gen(ccode)

xtset ccode year

* Here we calculate the average change in spending for different time periods
* both after and before the year of the survey.
quietly forval x=1/6 {
	forval y=1/5 {
		gen vart`y'`x'=F`y'.vart`x'
		gen dvart`y'`x'=(F`y'.vart`x'-vart`x')/vart`x'*100
		gen vartminus`y'`x'=L`y'.vart`x'
		gen dvartminus`y'`x'=(vart`x'-L`y'.vart`x')/vart`x'*100
		}

	egen dvartav13`x'=rowmean(dvart1`x' dvart2`x' dvart3`x')
	egen dvartav14`x'=rowmean(dvart1`x' dvart2`x' dvart3`x' dvart4`x')
	egen dvartav15`x'=rowmean(dvart1`x' dvart2`x' dvart3`x' dvart4`x' dvart5`x')
	egen dvartminus14`x'=rowmean(dvartminus1`x' dvartminus2`x'				///
	dvartminus3`x' dvartminus4`x')

	egen miss3`x'=rowmiss(dvart1`x' dvart2`x' dvart3`x')
	egen miss4`x'=rowmiss(dvart1`x' dvart2`x' dvart3`x' dvart4`x')
	egen miss5`x'=rowmiss(dvart1`x' dvart2`x' dvart3`x' dvart4`x' dvart5`x')

	replace dvartav13`x'=. if miss3`x'>1
	replace dvartav14`x'=. if miss4`x'>1
	replace dvartav15`x'=. if miss5`x'>1

	drop dvartminus1`x' dvartminus2`x' dvartminus3`x' dvartminus4`x'		///
	dvartminus5`x' miss3`x' miss4`x' miss5`x'
	}

rename *var*4 *var*_1
rename *var*5 *var*_2
rename *var*6 *var*_3

egen countryyear=group(country2 year)
reshape long vart vart1 vart2 vart3 vart4 vart5 vart_ vart1_ vart2_ vart3_	///
	vart4_ vart5_ vartminus1 vartminus2 vartminus3 vartminus4 vartminus5	///
	vartminus1_ vartminus2_ vartminus3_ vartminus4_ vartminus5_ dvart1		///
	dvart2 dvart3 dvart4 dvart5 dvart1_ dvart2_ dvart3_ dvart4_ dvart5_		///
	dvartav13 dvartav14 dvartav15 dvartav13_ dvartav14_ dvartav15_			///
	dvartminus14 dvartminus14_, i(countryyear) j(topic)

rename dvart*_* dpgdpt*
rename dvart* dpct*
rename vart*_* pgdpt*
rename vart* pct*
recode dpct* (.=0) if pct==0 & pct5==0
order pct-dpgdptminus14, alpha last
order pct* dpct* pgdpt* dpgdpt*, last
drop countryyear ccode

gen logpct=ln(pct)

* Austria is the only country-year where spending is missing while generosity is
* not. To obtain the same country-years for both variables, we use the 1985
* spending figures for Austria (the survey was conducted in 1986) and use the
* change between 1985 and 1990 as the average four-year change.
replace dpctav14=dpct4 if country2=="Austria" & year==1986 & topic!=1

tempfile oecd
save `oecd'

use `issp', clear

merge 1:1 country2 topic year using `oecd'
drop if _merge==2
drop _merge

save `issp', replace

/*------------------------------------------------------------------------------
 8. Adding data on GDP, growth and unemployment
------------------------------------------------------------------------------*/

* Note: we downloaded the data on all three indicators from the databank on the
* World Bank website, put them on one Excel sheet and copied them into Stata.
use "$data\WorldBank_2019.dta", clear

drop countrycode
rename countryname country2
replace country2="Great Britain" if country2=="United Kingdom"
replace country2="Slovakia" if country2=="Slovak Republic"
replace country2="South Korea" if country2=="Korea, Rep."

rename gdp* vart1*
rename growth* vart2*
rename unempilo* vart3*
rename unempne* vart4*

reshape long vart1 vart2 vart3 vart4, i(country2) j(year)
encode country2, gen(ccode)

* For Italy 1990, one of the unemployment variables is missing beforehand, while
* the other is missing for a few years afterwards. Since the two are identical
* in years where they are both present, we merge them. Sadly, the year 1990 is
* precisely the one year that is still missing, so we interpolate the data.
replace vart3=vart4 if country2=="Italy" & vart3==. & vart4!=.
replace vart3=(10.99+10.10)/2 if country2=="Italy" & year==1990

* Unemployment figures are missing for Austria 1985 and Great Britain 1985 and
* 1990 (both ILO and national estimates). We therefore supplement the data with
* figures from the OECD's Harmonised Unemployment Rates
* (https://stats.oecd.org/index.aspx?queryid=36324).
gen id=_n
recode id (3030=10.817) (3031=10.858) (3032=11.2) (3033=11.242)				///
	(3034=10.325) (3035=8.508) (3036=7.108) (3037=6.867) (else=.), gen(gb)
recode id (409=1.854) (410=2.516) (411=3.465) (412=3.8) (413=3.599) (414=	///
	3.135) (415=3.795) (416=3.564) (417=3.133) (418=3.24) (419=3.476)		///
	(else=.), gen(at)
replace vart3=gb if vart3==. & gb!=.
replace vart3=at if vart3==. & at!=.
drop id gb at

xtset ccode year

quietly forval x=1/4 {
	forval y=1/5 {
		gen vart`y'`x'=F`y'.vart`x'
		gen dvart`y'`x'=(F`y'.vart`x'-vart`x')/vart`x'*100
		}

	gen vartminus5`x'=L5.vart`x'
	egen dvartav13`x'=rowmean(dvart1`x' dvart2`x' dvart3`x')
	egen dvartav14`x'=rowmean(dvart1`x' dvart2`x' dvart3`x' dvart4`x')
	egen dvartav15`x'=rowmean(dvart1`x' dvart2`x' dvart3`x' dvart4`x' dvart5`x')

	egen miss3`x'=rowmiss(dvart1`x' dvart2`x' dvart3`x')
	egen miss4`x'=rowmiss(dvart1`x' dvart2`x' dvart3`x' dvart4`x')
	egen miss5`x'=rowmiss(dvart1`x' dvart2`x' dvart3`x' dvart4`x' dvart5`x')

	replace dvartav13`x'=. if miss3`x'>1
	replace dvartav14`x'=. if miss4`x'>1
	replace dvartav15`x'=. if miss5`x'>1

	drop dvart1`x' dvart2`x' dvart3`x' dvart4`x' dvart5`x' miss3`x' miss4`x' miss5`x'
	}

rename dvartav*1 dgdptav*
rename dvartav*2 dgrowthtav*
rename dvartav*3 dunemptav*
rename dvartav*4 dunempnetav*
rename vart*1 gdpt*
rename vart*2 growtht*
rename vart*3 unempt*
rename vart*4 unempnet*
order gdpt-dunempnetav15, alpha last
order gdpt* dgdpt* growtht* dgrowtht* unempt* dunempt* unempnet* dunempnet*, last
drop ccode

gen loggdpt=ln(gdpt)
gen loggdptminus5=ln(gdptminus5)

tempfile wb
save `wb', replace

use `issp', clear

merge m:1 country2 year using `wb'
format %18s country2
drop if _merge==2
drop _merge
order logpct, before(loggdpt)

* Note: we replace the ILO estimates of unemployment with the national estimates
* whenever the former is missing (before 1991). For country-years in our data
* where both are present, they are perfectly correlated.
foreach var in t t1 t2 t3 t4 t5 tminus5 {
	replace unemp`var'=unempne`var' if unemp`var'==. & unempne`var'!=.
	}

replace dunemptav13=dunempnetav13 if dunemptav13==. & dunempnetav13!=.
replace dunemptav14=dunempnetav14 if dunemptav14==. & dunempnetav14!=.
replace dunemptav15=dunempnetav15 if dunemptav15==. & dunempnetav15!=.

drop *unempne*

save `issp', replace

/*------------------------------------------------------------------------------
 9. Adding further country-level variables
------------------------------------------------------------------------------*/

use "$data2\qog_std_ts_jan19.dta", clear

gen country2=cname
replace country2="Cyprus" if country2=="Cyprus (1975-)"
replace country2="France" if country2=="France (1963-)"
replace country2="Great Britain" if country2=="United Kingdom"
replace country2="South Korea" if country2=="Korea, South"

* Age of democracy
* Note that there is another age of democracy variable (iaep_const), but the one
* we use has fewer missing values. (The two are correlated at 0.95.)
gen agedem=year-ccp_systyear
replace agedem=0 if mi(agedem)

keep year country2 agedem
tempfile qog
save `qog'

use `issp', clear
merge m:1 country2 year using `qog'
drop if _merge==2
drop _merge

save `issp', replace

* Federalism, effective number of parties, proportionality, government ideology
* and union density
use "$data\CPDS-1960-2017-Update-2019.dta", clear

gen country2=country
replace country2="Great Britain" if country2=="United Kingdom"
replace country2="United States" if country2=="USA"
encode country if inlist(countryn,1,2,3,5,9,11,12,13,14,16,17,18,19,22,23,	///
	24,25,26,28,32,33,34,35,36), gen(ctemp)
gen fedindex=lfed

forval x=1/24 {
	quietly sum fedindex if ctemp==`x' & year==1981
	recode fedindex (.=`r(mean)') if ctemp==`x' & year>1981
	}

rename fed fedcat
rename effpar_leg effpar
rename dis_gall gallindex
rename gov_party cabcomp
rename gov_left1 leftgov
rename gov_right1 rightgov

order ud, last

keep year country2 fedcat fedindex effpar gallindex cabcomp leftgov rightgov ud

tempfile cpds
save `cpds', replace

use `issp', clear
merge m:1 country2 year using `cpds'
drop if _merge==2
drop _merge

sort id

label var id "ID"
label var country "Country"
label var country2 "Country (string)"
label var year "Survey year"
label var wave "ISSP wave"
label var topic "Topic"
label var countryyear "Country-year"
label var countrytopic "Country-topic"
label var all "Preferences, overall"
label var t1 "Preferences 1st income tercile"
label var t2 "Preferences 2nd income tercile"
label var t3 "Preferences 3rd income tercile"
label var q1 "Preferences 1st income quintile"
label var q2 "Preferences 2nd income quintile"
label var q3 "Preferences 3rd income quintile"
label var q4 "Preferences 4th income quintile"
label var q5 "Preferences 5th income quintile"
label var p05 "Preferences 5th income percentile"
label var p10 "Preferences 10th income percentile"
label var p30 "Preferences 30th income percentile"
label var p50 "Preferences 50th income percentile"
label var p70 "Preferences 70th income percentile"
label var p90 "Preferences 90th income percentile"
label var p95 "Preferences 95th income percentile"
label var wsvall "Welfare state values, all"
label var wsv05 "Welfare state values, 5th percentile"
label var wsv10 "Welfare state values, 10th percentile"
label var wsv30 "Welfare state values, 30th percentile"
label var wsv50 "Welfare state values, 50th percentile"
label var wsv70 "Welfare state values, 70th percentile"
label var wsv90 "Welfare state values, 90th percentile"
label var wsv95 "Welfare state values, 95th percentile"
label var bcpall "Budget cutting preferences, all"
label var bcp05 "Budget-cutting preferences, 5th percentile"
label var bcp10 "Budget-cutting preferences, 10th percentile"
label var bcp30 "Budget-cutting preferences, 30th percentile"
label var bcp50 "Budget-cutting preferences, 50th percentile"
label var bcp70 "Budget-cutting preferences, 70th percentile"
label var bcp90 "Budget-cutting preferences, 90th percentile"
label var bcp95 "Budget-cutting preferences, 95th percentile"
label var gent "Generosity (t)"
label var gent1 "Generosity (t+1)"
label var gent2 "Generosity (t+2)"
label var gent3 "Generosity (t+3)"
label var gent4 "Generosity (t+4)"
label var gent5 "Generosity (t+5)"
label var gentminus1 "Generosity (t-1)"
label var gentminus2 "Generosity (t-2)"
label var gentminus3 "Generosity (t-3)"
label var gentminus4 "Generosity (t-4)"
label var gentminus5 "Generosity (t-5)"
label var dgent1 "Change in generosity (t+1)"
label var dgent2 "Change in generosity (t+2)"
label var dgent3 "Change in generosity (t+3)"
label var dgent4 "Change in generosity (t+4)"
label var dgent5 "Change in generosity (t+5)"
label var dgentav13 "Average change in generosity (t+1 - t+3)"
label var dgentav14 "Average change in generosity (t+1 - t+4)"
label var dgentav15 "Average change in generosity (t+1 - t+5)"
label var dgentminus14 "Average change in generosity (t-4 - t-1)"
label var pgent "Partial generosity (t)"
label var pgent1 "Partial generosity (t+1)"
label var pgent2 "Partial generosity (t+2)"
label var pgent3 "Partial generosity (t+3)"
label var pgent4 "Partial generosity (t+4)"
label var pgent5 "Partial generosity (t+5)"
label var dpgent1 "Change in partial generosity (t+1)"
label var dpgent2 "Change in partial generosity (t+2)"
label var dpgent3 "Change in partial generosity (t+3)"
label var dpgent4 "Change in partial generosity (t+4)"
label var dpgent5 "Change in partial generosity (t+5)"
label var dpgentav13 "Average change in partial generosity (t+1 - t+3)"
label var dpgentav14 "Average change in partial generosity (t+1 - t+4)"
label var dpgentav15 "Average change in partial generosity (t+1 - t+5)"
label var pct "Spending per capita (t)"
label var pct1 "Spending per capita (t+1)"
label var pct2 "Spending per capita (t+2)"
label var pct3 "Spending per capita (t+3)"
label var pct4 "Spending per capita (t+4)"
label var pct5 "Spending per capita (t+5)"
label var pctminus1 "Spending per capita (t-1)"
label var pctminus2 "Spending per capita (t-2)"
label var pctminus3 "Spending per capita (t-3)"
label var pctminus4 "Spending per capita (t-4)"
label var pctminus5 "Spending per capita (t-5)"
label var dpct1 "Change in spending per capita (t+1)"
label var dpct2 "Change in spending per capita (t+2)"
label var dpct3 "Change in spending per capita (t+3)"
label var dpct4 "Change in spending per capita (t+4)"
label var dpct5 "Change in spending per capita (t+5)"
label var dpctav13 "Average change in spending per capita (t+1 - t+3)"
label var dpctav14 "Average change in spending per capita (t+1 - t+4)"
label var dpctav15 "Average change in spending per capita (t+1 - t+5)"
label var dpctminus14 "Average change in spending per capita (t-4 - t-1)"
label var pgdpt "Spending in % GDP (t)"
label var pgdpt1 "Spending in % GDP (t+1)"
label var pgdpt2 "Spending in % GDP (t+2)"
label var pgdpt3 "Spending in % GDP (t+3)"
label var pgdpt4 "Spending in % GDP (t+4)"
label var pgdpt5 "Spending in % GDP (t+5)"
label var pgdptminus1 "Spending in % GDP (t-1)"
label var pgdptminus2 "Spending in % GDP (t-2)"
label var pgdptminus3 "Spending in % GDP (t-3)"
label var pgdptminus4 "Spending in % GDP (t-4)"
label var pgdptminus5 "Spending in % GDP (t-5)"
label var dpgdpt1 "Change in spending in % GDP (t+1)"
label var dpgdpt2 "Change in spending in % GDP (t+2)"
label var dpgdpt3 "Change in spending in % GDP (t+3)"
label var dpgdpt4 "Change in spending in % GDP (t+4)"
label var dpgdpt5 "Change in spending in % GDP (t+5)"
label var dpgdptav13 "Average change in spending in % GDP (t+1 - t+3)"
label var dpgdptav14 "Average change in spending in % GDP (t+1 - t+4)"
label var dpgdptav15 "Average change in spending in % GDP (t+1 - t+5)"
label var dpgdptminus14 "Average change in spending in % GDP (t-4 - t-1)"
label var gdpt "GDP per capita (t)"
label var gdpt1 "GDP per capita (t+1)"
label var gdpt2 "GDP per capita (t+2)"
label var gdpt3 "GDP per capita (t+3)"
label var gdpt4 "GDP per capita (t+4)"
label var gdpt5 "GDP per capita (t+5)"
label var gdptminus5 "GDP per capita (t-5)"
label var dgdptav13 "Average change in GDP per capita (t+1 - t+3)"
label var dgdptav14 "Average change in GDP per capita (t+1 - t+4)"
label var dgdptav15 "Average change in GDP per capita (t+1 - t+5)"
label var growtht "GDP % growth (t)"
label var growtht1 "GDP % growth (t+1)"
label var growtht2 "GDP % growth (t+2)"
label var growtht3 "GDP % growth (t+3)"
label var growtht4 "GDP % growth (t+4)"
label var growtht5 "GDP % growth (t+5)"
label var growthtminus5 "GDP % growth (t-5)"
label var dgrowthtav13 "Average change in GDP growth (t+1 - t+3)"
label var dgrowthtav14 "Average change in GDP growth (t+1 - t+4)"
label var dgrowthtav15 "Average change in GDP growth (t+1 - t+5)"
label var unempt "Unemployment % (t)"
label var unempt1 "Unemployment % (t+1)"
label var unempt2 "Unemployment % (t+2)"
label var unempt3 "Unemployment % (t+3)"
label var unempt4 "Unemployment % (t+4)"
label var unempt5 "Unemployment % (t+5)"
label var unemptminus5 "Unemployment % (t-5)"
label var dunemptav13 "Average change in unemployment (t+1 - t+3)"
label var dunemptav14 "Average change in unemployment (t+1 - t+4)"
label var dunemptav15 "Average change in unemployment (t+1 - t+5)"
label var logpct "Spending per capita, logged (t)"
label var loggdpt "GDP per capita, logged (t)"
label var loggdptminus5 "GDP per capita, logged (t-5)"
label var agedem "Age of democracy"
label var effpar "Effective number of parties"
label var fedcat "Federalism (categories)"
label var fedindex "Federalism (index)"
label var ud "Union density"

save "$data\Schakel_2020_Chapter2_1_Data.dta", replace
